package br.com.aquinoevasconcelos.servlets.contato;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.aquinoevasconcelos.excecao.ValidacaoException;
import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.modelos.Contato;

@WebServlet("/InserirContato")
public class InserirContato extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public InserirContato() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Fachada fachada = Fachada.getInstancia();
		Contato contato = new Contato();

		contato.setNome(request.getParameter("nomeCompleto"));
		contato.setEmail(request.getParameter("email"));
		contato.setAssunto(request.getParameter("assunto"));
		contato.setDescricao(request.getParameter("descricao"));

		try {

			fachada.inserirContato(contato);
			response.sendRedirect("contato/contato.jsp?msg=sucesso");

		} catch (ValidacaoException e) {
			System.out.println("erro na aplicacao");

			response.sendRedirect("pagina_erro/erro.jsp");

			/*
			 * RequestDispatcher rd = request
			 * .getRequestDispatcher("/index.jsp"); rd.forward(request,
			 * response); e.printStackTrace();
			 */
		}

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

}
